package cn.iocoder.yudao.module.worker.controller.admin.trend;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.worker.controller.admin.trend.vo.*;
import cn.iocoder.yudao.module.worker.dal.dataobject.trend.WorkerTrendDO;
import cn.iocoder.yudao.module.worker.service.trend.WorkerTrendService;

@Tag(name = "管理后台 - 店员动态")
@RestController
@RequestMapping("/worker/trend")
@Validated
public class WorkerTrendController {

    @Resource
    private WorkerTrendService trendService;

    @PostMapping("/create")
    @Operation(summary = "创建店员动态")
    @PreAuthorize("@ss.hasPermission('worker:trend:create')")
    public CommonResult<Long> createTrend(@Valid @RequestBody WorkerTrendSaveReqVO createReqVO) {
        return success(trendService.createTrend(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店员动态")
    @PreAuthorize("@ss.hasPermission('worker:trend:update')")
    public CommonResult<Boolean> updateTrend(@Valid @RequestBody WorkerTrendSaveReqVO updateReqVO) {
        trendService.updateTrend(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店员动态")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('worker:trend:delete')")
    public CommonResult<Boolean> deleteTrend(@RequestParam("id") Long id) {
        trendService.deleteTrend(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员动态")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('worker:trend:query')")
    public CommonResult<WorkerTrendRespVO> getTrend(@RequestParam("id") Long id) {
        WorkerTrendDO trend = trendService.getTrend(id);
        return success(BeanUtils.toBean(trend, WorkerTrendRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员动态分页")
    @PreAuthorize("@ss.hasPermission('worker:trend:query')")
    public CommonResult<PageResult<WorkerTrendRespVO>> getTrendPage(@Valid WorkerTrendPageReqVO pageReqVO) {
        PageResult<WorkerTrendDO> pageResult = trendService.getTrendPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkerTrendRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店员动态 Excel")
    @PreAuthorize("@ss.hasPermission('worker:trend:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTrendExcel(@Valid WorkerTrendPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkerTrendDO> list = trendService.getTrendPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "店员动态.xls", "数据", WorkerTrendRespVO.class,
                        BeanUtils.toBean(list, WorkerTrendRespVO.class));
    }

}